package com.ymdd.library.permission;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import com.ymdd.library.R;

/**
 * Created by 000286 on 2017/9/22.
 * Description
 */

public final class RationaleDialog {
  private AlertDialog.Builder mBuilder = null;

  private Rationale mRationale = null;

  private DialogInterface.OnClickListener mOnClickListener = new DialogInterface.OnClickListener() {
    @Override public void onClick(DialogInterface mDialogInterface, int which) {
      switch (which) {
        case DialogInterface.BUTTON_NEGATIVE:
          mRationale.cancel();
          break;
        case DialogInterface.BUTTON_POSITIVE:
          mRationale.resume();
          break;
      }
    }
  };

  public RationaleDialog(@NonNull Context mContext, @NonNull Rationale mRationale) {
    this.mBuilder = new AlertDialog.Builder(mContext).setCancelable(false)
        .setTitle(R.string.permission_title_permission_rationale)
        .setMessage(R.string.permission_message_permission_rationale)
        .setPositiveButton(R.string.permission_resume, mOnClickListener)
        .setNegativeButton(R.string.permission_cancel, mOnClickListener);
    this.mRationale = mRationale;
  }

  @NonNull public RationaleDialog setTitle(@NonNull String title) {
    mBuilder.setTitle(title);
    return this;
  }

  @NonNull public RationaleDialog setTitle(@NonNull int title) {
    mBuilder.setTitle(title);
    return this;
  }

  @NonNull public RationaleDialog setMessage(@NonNull String message) {
    mBuilder.setMessage(message);
    return this;
  }

  @NonNull public RationaleDialog setMessage(@StringRes int message) {
    mBuilder.setMessage(message);
    return this;
  }

  @NonNull public RationaleDialog setNegativeButton(@NonNull String text,
      @Nullable DialogInterface.OnClickListener negativeListener) {
    mBuilder.setNegativeButton(text, negativeListener);
    return this;
  }

  @NonNull public RationaleDialog setNegativeButton(@StringRes int text,
      @Nullable DialogInterface.OnClickListener negativeListener) {
    mBuilder.setNegativeButton(text, negativeListener);
    return this;
  }

  @NonNull public RationaleDialog setPositiveButton(@NonNull String text) {
    mBuilder.setPositiveButton(text, mOnClickListener);
    return this;
  }

  @NonNull public RationaleDialog setPositiveButton(@StringRes int text) {
    mBuilder.setPositiveButton(text, mOnClickListener);
    return this;
  }

  public void show() {
    mBuilder.show();
  }
}
